package com.wgchao.identityrightsmanagement.query.vo;

import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseAccount;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseAgency;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseDepartment;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseAccountRepository;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseAgencyRepository;
import com.wgchao.identityrightsmanagement.infrastructure.mapstruct.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @ClassName: DepartmentVoConvert
 * @author: wgchao
 * @createTime: 2020/6/16 11:44 AM
 * @Description: TODO
 */
@Component
public class DepartmentVoConvert {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private SysBaseAccountRepository sysBaseAccountRepository;
    @Autowired
    private SysBaseAgencyRepository sysBaseAgencyRepository;

    public DepartmentVo convert(SysBaseDepartment sysBaseDepartment){
        DepartmentVo departmentVo = departmentMapper.toT(sysBaseDepartment);
        if(!StringUtils.isEmpty(sysBaseDepartment.getCreator())){
            SysBaseAccount creator = sysBaseAccountRepository.findFirstByUsername(sysBaseDepartment.getCreator());
            departmentVo.setCreatorName(creator == null ? null : creator.getName());
        }
        if(!StringUtils.isEmpty(sysBaseDepartment.getLastUpdator())){
            SysBaseAccount updator = sysBaseAccountRepository.findFirstByUsername(sysBaseDepartment.getLastUpdator());
            departmentVo.setLastUpdatorName(updator == null ? null : updator.getName());
        }
        SysBaseAgency sysBaseAgency = sysBaseAgencyRepository.findFirstByAgencyCode(sysBaseDepartment.getAgencyCode());
        departmentVo.setAgencyName(sysBaseAgency == null ? null : sysBaseAgency.getAgencyName());
        return departmentVo;
    }

}
